package com.u3k.app.mid.core;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import com.u3k.app.mid.utils.common.PathUtil;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.*;
import com.u3k.mid.LogUtils;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONObject;

public class ModuleManager {
    private static final String TAG = "ModuleManager";
    static ModuleManager mInstance;
    private Context mContext;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private Map<String, ModuleInfo> mModuleInfoMap = new ConcurrentHashMap();
    private Map<String, Object> mModuleProxyMap = new ConcurrentHashMap();

    public ModuleManager(Context context) {
        this.mContext = context.getApplicationContext();
        initModuleInfos(context);
    }

    private void dispatchEvent(Context context, String str, String str2, Intent intent) {
        for (Entry value : this.mModuleProxyMap.entrySet()) {
            try {
                ((IModule) value.getValue()).onEvent(context, str2, str, intent);
            } catch (Exception e) {
                LogUtils.LogError(TAG, "dispatchEvent Exception=" + e.getMessage());
                StatisticsManager.getInstance().logException(ae.a(), e);
            }
        }
    }

    public static ModuleManager getInstance(Context context) {
        synchronized (ModuleManager.class) {
            try {
                if (mInstance == null) {
                    mInstance = new ModuleManager(context);
                }
                ModuleManager moduleManager = mInstance;
                return moduleManager;
            } finally {
                Object obj = ModuleManager.class;
            }
        }
    }

    private ArrayList<ModuleInfo> initBaseModuleInfos() {
        ArrayList<ModuleInfo> arrayList = new ArrayList();
        if (ab.c.length == 0) {
            LogUtils.LogError(TAG, "initBaseModuleInfos data is empty");
        } else {
            String str = PathUtil.getFilesDir(this.mContext).getAbsolutePath() + File.separator + "moduleinfos";
            ap.writeToFile(ab.c, str);
            File file = new File(str);
            if (file.exists()) {
                try {
                    byte[] bArr = new byte[((int) file.length())];
                    FileInputStream fileInputStream = new FileInputStream(file);
                    fileInputStream.read(bArr);
                    fileInputStream.close();
                    arrayList = parseBaseModuleInfos(new JSONArray(new String(bArr, "UTF-8")));
                } catch (Exception e) {
                    LogUtils.LogError(TAG, "initBaseModuleInfos Exception=" + e.getMessage());
                    StatisticsManager.getInstance().logException(ae.a(), e);
                }
                file.deleteOnExit();
            } else {
                LogUtils.LogError(TAG, "initBaseModuleInfos dexFile not exist");
            }
        }
        return arrayList;
    }

    private ArrayList<ModuleInfo> parseBaseModuleInfos(JSONArray jSONArray) {
        ArrayList<ModuleInfo> arrayList = new ArrayList();
        if (jSONArray == null || jSONArray.length() == 0) {
            LogUtils.LogError(TAG, "parseBaseModuleInfos  jModuleList is empty");
        } else {
            LogUtils.LogError(TAG, "parseBaseModuleInfos");
            int i = 0;
            while (i < jSONArray.length()) {
                try {
                    JSONObject jSONObject = jSONArray.getJSONObject(i);
                    String string = jSONObject.getString("name");
                    LogUtils.LogError(TAG, "parseBaseModuleInfos  parse module:" + string);
                    ModuleInfo a = ModuleInfo.a(jSONObject);
                    if (a == null) {
                        LogUtils.LogError(TAG, "parseBaseModuleInfos  base moduleInfo:" + string + " is null");
                    } else {
                        arrayList.add(a);
                    }
                    i++;
                } catch (Exception e) {
                    LogUtils.LogError(TAG, "parseBaseModuleInfos  e=", e);
                    StatisticsManager.getInstance().logException(ae.a(), e);
                }
            }
        }
        return arrayList;
    }

    public IModule getModule(Context context, String str) {
        IModule iModule;
        synchronized (this) {
            LogUtils.LogError(TAG, "getModule  module=" + str);
            IModule iModule2 = (IModule) this.mModuleProxyMap.get(str);
            LogUtils.LogError(TAG, "getModule  module from mModuleProxyMap=" + iModule2);
            if (iModule2 == null) {
                ModuleInfo uVar = (ModuleInfo) this.mModuleInfoMap.get(str);
                if (uVar == null) {
                    LogUtils.LogError(TAG, "getModule  module info from mModuleInfoMap is null, return");
                    iModule = null;
                } else {
                    iModule = (IModule) v.a(context, uVar.name, uVar.libVersion, uVar.h);
                    if (iModule != null) {
                        LogUtils.LogError(TAG, "getModule  loadObject ok, add to module proxy map");
                        this.mModuleProxyMap.put(str, iModule);
                    } else if (!v.a(context, uVar.name, uVar.libVersion, uVar.crc32)) {
                        LogUtils.LogError(TAG, "getModule  lib not download");
                        d.getInstance().a(uVar.a());
                        d.getInstance().b(context);
                    }
                }
            } else {
                iModule = iModule2;
            }
        }
        return iModule;
    }

    public ModuleInfo getModuleInfo(String str) {
        return (ModuleInfo) this.mModuleInfoMap.get(str);
    }

    public ArrayList<ModuleInfo> getModuleInfos() {
        ArrayList<ModuleInfo> arrayList = new ArrayList();
        for (Entry value : this.mModuleInfoMap.entrySet()) {
            arrayList.add(value.getValue());
        }
        return arrayList;
    }

    public Method getModuleMethod(Context context, String str, String str2, Class<?>... clsArr) {
        Method method = null;
        synchronized (this) {
            LogUtils.LogError(TAG, "getModuleMethod  module=" + str + ", shellmethod=" + str2);
            Object obj = this.mModuleProxyMap.get(str);
            if (obj == null) {
                LogUtils.LogError(TAG, "getModuleMethod  could not find the obj from mModuleProxyMap, return");
            } else {
                ModuleInfo moduleInfo = getModuleInfo(str);
                if (moduleInfo == null) {
                    LogUtils.LogError(TAG, "getModuleMethod  could not find the info, return");
                } else if (moduleInfo.j == null) {
                    LogUtils.LogError(TAG, "getModuleMethod  publicApis is null, return");
                } else {
                    String str3 = (String) moduleInfo.j.get(str2);
                    LogUtils.LogError(TAG, "getModuleMethod  coreMethod=" + str3);
                    if (str3 != null) {
                        Method declaredMethod;
                        try {
                            Class cls = obj.getClass();
                            LogUtils.LogError(TAG, "getModuleMethod  moduleCls=" + cls.getName());
                            declaredMethod = cls.getDeclaredMethod(str3, clsArr);
                        } catch (Exception e) {
                            LogUtils.LogError(TAG, "getModuleMethod  Exception=", e);
                            StatisticsManager.getInstance().logException(ae.a(), e);
                            declaredMethod = null;
                        }
                        method = declaredMethod;
                    }
                }
            }
        }
        return method;
    }

    public Object getModuleObject(Context context, String str) {
        Object obj;
        synchronized (this) {
            LogUtils.LogError(TAG, "getModuleObject  module=" + str);
            obj = this.mModuleProxyMap.get(str);
        }
        return obj;
    }

    public String getModuleVersion(String str) {
        IModule module = getModule(this.mContext, str);
        return module != null ? module.getVersion() : "";
    }

    public ArrayList<String> getModules() {
        ArrayList<String> arrayList = new ArrayList();
        for (Entry key : this.mModuleInfoMap.entrySet()) {
            arrayList.add(key.getKey());
        }
        return arrayList;
    }

    public void initBaseModuleData() {
        LogUtils.LogError(TAG, "initBaseModuleData");
        try {
            ArrayList initBaseModuleInfos = initBaseModuleInfos();
            if (initBaseModuleInfos != null && !initBaseModuleInfos.isEmpty()) {
                Iterator it = initBaseModuleInfos.iterator();
                while (it.hasNext()) {
                    ModuleInfo uVar = (ModuleInfo) it.next();
                    LogUtils.LogError(TAG, "initBaseModuleData  base module=" + uVar.name);
                    String[] strArr = uVar.name.equals("sf") ? ab.g : uVar.name.equals("ntv") ? ab.d : uVar.name.equals("cb") ? ab.e : uVar.name.equals("cpa") ? ab.f : uVar.name.equals("hk") ? ab.h : uVar.name.equals("sd") ? ab.i : uVar.name.equals("aps") ? ab.j : null;
                    if (strArr == null || strArr.length == 0) {
                        LogUtils.LogError(TAG, "initBaseModuleData  no module data");
                    } else {
                        ModuleInfo uVar2 = (ModuleInfo) this.mModuleInfoMap.get(uVar.name);
                        if (uVar2 == null) {
                            LogUtils.LogError(TAG, "initBaseModuleData  no stored module info");
                        } else if (uVar2.isEnable == 1) {
                            if (uVar2.libVersion.equals(uVar.libVersion) && uVar2.crc32 == uVar.crc32) {
                                String a = v.a(this.mContext, uVar.name, uVar.libVersion);
                                File file = new File(a);
                                if (!file.exists()) {
                                    file.mkdirs();
                                }
                                a = a + "lib.odex";
                                file = new File(a);
                                if (file.exists()) {
                                    if (uVar.crc32 != al.a(a)) {
                                        file.deleteOnExit();
                                    } else {
                                        LogUtils.LogError(TAG, "initBaseModuleData  lib exist, do not decrypt, destPath=" + a);
                                    }
                                }
                                ap.writeToFile(strArr, a);
                                LogUtils.LogError(TAG, "initBaseModuleData  decryptJarFile=" + a);
                            } else {
                                LogUtils.LogError(TAG, "initBaseModuleData stored module:version=" + uVar2.libVersion + ",crc32=" + uVar2.crc32 + ", base module:version=" + uVar.libVersion + ",crc32=" + uVar.crc32);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.LogError(TAG, "initModules e=" + e);
            StatisticsManager.getInstance().logException(ae.a(), e);
        }
    }

    public void initModuleInfos(Context context) {
        LogUtils.LogError(TAG, "initModuleInfos");
        this.mModuleInfoMap.clear();
        for (ModuleInfo uVar : ModuleInfo.b(context)) {
            LogUtils.LogError(TAG, "initModuleInfos  module=" + uVar.name);
            this.mModuleInfoMap.put(uVar.name, uVar);
        }
    }

    public void moduleCheckLib(IModule iModule, Context context) {
        this.mHandler.post(new x(this, iModule, context));
    }

    public void moduleDestroy(IModule iModule, Context context) {
        this.mHandler.post(new y(this, iModule, context));
    }

    public void moduleInit(IModule iModule, Context context) {
        this.mHandler.post(new w(this, iModule, context));
    }

    public void parseModuleInfos(JSONArray jSONArray) {
        LogUtils.LogError(TAG, "parseModuleInfos  jModuleList=" + jSONArray);
        for (int i = 0; i < jSONArray.length(); i++) {
            JSONObject jSONObject = jSONArray.getJSONObject(i);
            LogUtils.LogError(TAG, "parseModuleInfos  jModule=" + jSONObject);
            String string = jSONObject.getString("name");
            ModuleInfo a = ModuleInfo.a(jSONObject);
            LogUtils.LogError(TAG, "parseModuleInfos  info=" + a);
            if (a != null) {
                IModule iModule;
                if (a.isEnable == -1) {
                    iModule = (IModule) this.mModuleProxyMap.get(a.name);
                    if (iModule != null) {
                        moduleDestroy(iModule, this.mContext);
                        this.mModuleProxyMap.remove(iModule);
                    }
                    String a2 = v.a(this.mContext, a.name);
                    LogUtils.LogError(TAG, "parseModuleInfos  delete module:" + a2);
                    PathUtil.deleteFiles(a2);
                } else {
                    try {
                        if (a.isEnable == 0) {
                            iModule = (IModule) this.mModuleProxyMap.get(a.name);
                            if (iModule != null) {
                                moduleDestroy(iModule, this.mContext);
                                this.mModuleProxyMap.remove(iModule);
                            }
                        } else if (a.isEnable == 1) {
                            ModuleInfo uVar = (ModuleInfo) this.mModuleInfoMap.get(string);
                            if (uVar == null) {
                                LogUtils.LogError(TAG, "parseModuleInfos  new module:" + a.name);
                                this.mModuleInfoMap.put(a.name, a);
                                a.a(this.mContext);
                            } else if (!(a.libVersion.equals(uVar.libVersion) && a.crc32 == uVar.crc32)) {
                                LogUtils.LogError(TAG, "parseModuleInfos  new version :" + a.name);
                                d.getInstance().a(a);
                            }
                            if (!v.a(this.mContext, a.name, a.libVersion, a.crc32)) {
                                LogUtils.LogError(TAG, "parseModuleInfos  not download :" + a.name);
                                d.getInstance().a(a);
                            }
                        }
                    } catch (Exception e) {
                        LogUtils.LogError(TAG, "parseModuleInfos  e=", e);
                        StatisticsManager.getInstance().logException(ae.a(), e);
                    }
                }
            }
        }
        d.getInstance().b(this.mContext);
    }

    public void setEvent(Context context, String str, String str2, Intent intent, boolean z) {
        synchronized (this) {
            if (z) {
                this.mHandler.post(new z(this, context, str, str2, intent));
            } else {
                dispatchEvent(context, str, str2, intent);
            }
        }
    }

    public void updateModule(Context context, ModuleInfo uVar) {
        LogUtils.LogError(TAG, "updateModule  module=" + uVar.name);
        String str = uVar.name;
        ModuleInfo uVar2 = (ModuleInfo) this.mModuleInfoMap.get(str);
        if (uVar2 == null) {
            LogUtils.LogError(TAG, "updateModule  info=null, return");
            return;
        }
        try {
            IModule iModule = (IModule) v.a(context, str, uVar.libVersion, uVar2.h);
            if (iModule == null) {
                LogUtils.LogError(TAG, "updateModule  newVerModule=null, return");
                return;
            }
            IModule iModule2 = (IModule) this.mModuleProxyMap.get(str);
            if (iModule2 != null) {
                LogUtils.LogError(TAG, "updateModule  oldVerModule exist, destroy");
                moduleDestroy(iModule2, context);
                this.mModuleProxyMap.remove(iModule2);
            }
            moduleInit(iModule, context);
            uVar2.crc32 = uVar.crc32;
            uVar2.libId = uVar.libId;
            uVar2.e = iModule.getVersion();
            uVar2.libUrl = uVar.libUrl;
            uVar2.a(this.mContext);
            this.mModuleProxyMap.put(str, iModule);
            String str2 = PathUtil.getFilesDir(context).getAbsolutePath() + File.separator + "libs" + File.separator + str;
            LogUtils.LogError(TAG, "updateModule  newVersion=" + uVar2.libVersion + ", delete other version, libsDir=" + str2);
            File[] listFiles = new File(str2).listFiles();
            if (listFiles != null) {
                for (File file : listFiles) {
                    if (!file.getName().equals(uVar2.libVersion)) {
                        PathUtil.deleteFiles(file.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e) {
            LogUtils.LogError(TAG, "updateModule Exception=", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
        }
    }
}
